﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Tanmia.Basis.ViewInterface;
using Tanmia.Basis.ViewModel;
using Tanmia.Warehouse.Core;
using Tanmia.Warehouse.Domain;

namespace Tanmia.Warehouse.Presentation.Scenarios
{
    [ViewInfo("استعراض شجرة المستودعات", Constants.RegionMain, Views.SectionTreeBrowser)]
    public class SectionTreeBrowserViewModel : BrowserViewModel<Section>
    {
        public ICollectionView Branchs { get; private set; }

        public IEnumerable<TreeViewItem> TreeItems { get; protected set; }

        public SectionDomain SectionObj
        {
            get { return base.EntityObj as SectionDomain; }
        }

        public BranchDomain BranchObj { get; private set; }

        public SectionTreeBrowserViewModel(IShellView shellView, IRegionManager regionManager, IEventAggregator eventAggregator, IMessageService messageService,
            SectionDomain sectionObj, BranchDomain branchObj)
            : base(shellView, regionManager, eventAggregator, messageService, sectionObj)
        {
            EntityName = "Section";

            this.BranchObj = branchObj;
        }

        protected override void CoreRefresh()
        {
            Branchs = CollectionViewSource.GetDefaultView(BranchObj.Gets());
            base.CoreRefresh();
        }

        protected override void CompleteRefresh()
        {
            BuildSectionTree();
            base.CompleteRefresh();
        }

        private void BuildSectionTree()
        {
            TreeViewItem root = new TreeViewItem { Header = "المؤسسة" };

            foreach (Branch branch in Branchs)
            {
                TreeViewItem tvItem = new TreeViewItem { Header = branch.Name, DataContext = branch };
                IList<Section> children = GetParentSections(branch);
                PopulateSectionItems(tvItem, children);
                root.Items.Add(tvItem);
            }

            TreeItems = new TreeViewItem[] { root };
            RaisePropertyChanged(() => TreeItems);
        }

        private IList<Section> GetParentSections(Branch branch)
        {
            return ((IList<Section>)Entities.SourceCollection)
                .Where(x => x.Branch.BranchID == branch.BranchID && x.Parent == null)
                .ToList<Section>();
        }

        private void PopulateSectionItems(TreeViewItem tvItem, IList<Section> children)
        {
            IList<Section> items = null;
            TreeViewItem treeItem = null;

            foreach (Section child in children)
            {
                items = GetChildSections(child);
                if (items.Count > 0)
                {
                    treeItem = new TreeViewItem { Header = child.Name, DataContext = child };
                    PopulateSectionItems(treeItem, items);
                    tvItem.Items.Add(treeItem);
                }
                else
                {
                    treeItem = new TreeViewItem { Header = child.Name };
                    tvItem.Items.Add(treeItem);
                }
            }
        }

        private IList<Section> GetChildSections(Section child)
        {
            return ((IList<Section>)Entities.SourceCollection)
                .Where(x => x.Parent != null && x.Parent.SectionID == child.SectionID)
                .ToList<Section>();
        }
    }
}
